package com.ananjt.anantool;

import java.util.concurrent.CountDownLatch;

class StartTread implements Runnable {

    public CountDownLatch cd;

    public StartTread(CountDownLatch cd) {
        this.cd = cd;
    }

    @Override
    public void run() {
        System.out.println("子线程," + Thread.currentThread().getName() + "开始执行...");
        cd.countDown();
        System.out.println("子线程," + Thread.currentThread().getName() + "结束执行...");
    }
}

class EndTread implements Runnable {

    public CountDownLatch cd;

    public EndTread(CountDownLatch cd) {
        this.cd = cd;
    }

    @Override
    public void run() {
        System.out.println("子线程," + Thread.currentThread().getName() + "开始执行...");
        cd.countDown();
        System.out.println("子线程," + Thread.currentThread().getName() + "结束执行...");
    }
}


public class ThredPack {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch cd = new CountDownLatch(2);
        new Thread(new StartTread(cd)).start();
        new Thread(new EndTread(cd)).start();
        // 调用当前方法主线程阻塞  countDown结果为0, 阻塞变为运行状态
        cd.await();
        System.out.println("两个子线程执行完毕....");
        System.out.println("继续主线程执行..");

    }
}
